package com.stoken.web.control;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stoken.domain.MemberBaseInfo;
import com.stoken.domain.MemberLeaveMessage;
import com.stoken.service.MemberLeaveMessageService;
import com.stoken.service.MemberLeaveReplyService;
import com.stoken.web.StokenUtils;
import com.symbio.commons.Compositor;
import com.symbio.commons.Compositor.CompositorType;
import com.symbio.commons.Page;
import com.symbio.utils.WebUtils;

@Controller
@RequestMapping("/leaveReply")
public class MemberLeaveReplyControl {
	
	@Autowired
	private MemberLeaveReplyService memberReplyService;
	
	@Autowired
	private MemberLeaveMessageService memberLeaveMessageService;
	
	/**
	 * 加载留言管理页面
	 * @return
	 */
	@RequestMapping("/backstage/loadLeaveMessagePage")
	public String loadLeaveMessage(){
		return "/backstage/leaveMessage/leaveMessage";		
	}
	
	@RequestMapping("/backstage/leaveMessageData")
	public String loadLeaveMeData(HttpServletRequest request,ModelMap modeMape){
		//MemberBaseInfo mbi = (MemberBaseInfo) request.getSession().getAttribute(StokenUtils.LOGINACCOUNT);
		Page<MemberLeaveMessage> pageData = new Page<MemberLeaveMessage>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("leaveTime",CompositorType.DESC);
		pageData.setCompositor(compositor);
		
		pageData = memberLeaveMessageService.findAll(pageData);
	    modeMape.addAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		modeMape.addAttribute("pagination", page.showPages());
		
		return "/backstage/leaveMessage/leaveMessagePage";		
	}
	
	@RequestMapping("/backstage/replay")
	public String loadLeaveReply(HttpServletRequest request,long id,ModelMap modeMape){		
		MemberLeaveMessage mbLeaveMessage = memberLeaveMessageService.get(id);
		if(mbLeaveMessage == null){
			return "/backstage/leaveMessage/leaveMessage";		
		}
		modeMape.addAttribute("mbLeaveMessage", mbLeaveMessage);
		return "/backstage/leaveMessage/replay";
		
	}
	
	@RequestMapping("/backstage/writeRplay")
	public String replayLeaveMessage(HttpServletRequest request,ModelMap modeMape,String reContext,long leaveId){
		if(StringUtils.isNotBlank(reContext)){
			memberReplyService.writeLeaveMessage(reContext, leaveId);
		}
		return "/backstage/leaveMessage/leaveMessage";		
	}
	
	
}
